<?php
/**
 * Created by PhpStorm.
 * User: xeng
 * Date: 2018/1/29
 * Time: 14:05
 */
/*
 * 账号冻结 类
 */
namespace Home\Controller;
use Think\Controller;
class FreezeController extends AuthController{
    /*
     * 默认首页
     * 查询所有 被冻结 的会员
     */
    private  $page_count=10;
    public function index(){
        /*if($_POST){
            $this->CheckPower('7001'); 
        }
        $m= $this->page_count;
        $condi=$_GET;
        $condi=array_merge($condi,$_POST);
        $p = $_GET['p'] ? $_GET['p'] : 1;         //第几页
        $firstRow = ((int)$p-1) * $this->page_count;
        $where = ' 1=1 ';
        if($condi['phonenum']){
            $condi['phonenum']=trim($condi['phonenum']);
            $where  .=" and phonenum='{$condi['phonenum']}' ";
        }
        if($condi['gameid']){
            $condi['gameid']=trim($condi['gameid']);
            $where .=" and gameid='{$condi['gameid']}' ";
        } 

        $lists = $this->game("SELECT  * FROM data_useraccount  where $where and  disablelogin=1  limit $firstRow,$m");
        $count = $this->game("SELECT count(*) FROM data_useraccount  where $where and  disablelogin=1 ");
       
        foreach ($lists as $key => $value) {
            $accid=$value["accountid"];
            $results=$this->weblog("SELECT datetime,operator,content from ltq_disablelogin where accountid=$accid");
            $lists[$key]['disabletime']= $results[0]['datetime']; 
            $lists[$key]['optid']=$results[0]['operator']; 
            $lists[$key]['content']=$results[0]['content']; 
        } 


        if(!$condi['phonenum'] && !$condi['gameid']){
            $r=$this-> actionLog('冻结玩家','查询了所有','信息');
        }else{
            $s='';
            if(!$condi['phonenum']){$s .=$condi['phonenum'];}
            if(!$condi['gameid']){$s .=','.$condi['gameid'];} 
            $r=$this-> actionLog($condi['gameid'],'查询了gameid为','用户的冻结详情',trim($s,','));  
        }
        $p=$_GET['p']?$_GET['p']:1;
        $page_show =$this->page_show($count,$this->page_count,$p);
        $shows=$page_show;
        $pg=array_pop($shows);
        $this->assign('totalPages', $pg);
        $this->assign("p",$p);
        $this->assign("count",reset(reset($count)));
        // $this->assign("page_show",$page_show);
        $this->assign("condi",$condi);
        $this->assign('lists',$lists);*/
        $this->CheckPower('7001');
        $this->display();
    }


    public function getlist(){
        S(array('type'=>'file','prefix'=>'disablelogin','expire'=>60*60*24));
        $useraccount = M();
        $commands = M('','','DB_CONFIG1');
        $url = pageurl(__SELF__);
        $pagesize = empty($url['limit'])?10:$url['limit'];
        $page = empty($url['page'])?1:$url['page'];
        $state = ($page-1)*$pagesize;
       // $where['_string'] = 'ongameid IS null'; //默认查询离线


        if($url['gameid'] > 0 && !empty($url['gameid'])){
            $where['gameid'] = array('eq',$url['gameid']);
        }

        if(!empty($url['phonenum'])){
            $where['phonenum'] = array('eq',$url['phonenum']);
        }

        $list = S('disablelogin_list_'.$page.$pagesize);
        $count = S('disablelogin_count');
        $count2 =$useraccount->table('ltq_disablelogin')->where($where)->count();
        if($count < 0 || $count !=$count2 || empty($list) || !empty($url['gameid'])){
            $list = $useraccount->table('ltq_disablelogin')->where($where)->limit($state,$pagesize)->select();
            S('disablelogin_list_'.$page.$pagesize,$list);
            $count = $count2;
            S('disablelogin_count',$count);
        }
        $data['message'] = '';
        $data['code'] = 0;

        $data['count'] = $count;
        $data['data'] = $list;
        $this->ajaxReturn($data,'json');
    }



    public function getone(){
        $useraccount = M('','','DB_CONFIG1');
        $url = pageurl(__SELF__);
        if($url['gameid'] > 0 && !empty($url['gameid'])){
            $where['gameid'] = array('eq',$url['gameid']);
        }

        if(!empty($url['phonenum'])){
            $where['phonenum'] = array('eq',$url['phonenum']);
        }
        $list=[];
        if(!empty($where)){
            $list = $useraccount->table('data_useraccount')->field('nickname,gameid,disablelogin,phonenum,registertime')->where($where)->select();
        }

        $data['code'] = 0;
        $data['message'] = '';
        $data['count'] = 1;
        $data['data'] = $list;
        $this->ajaxReturn($data,'json');
    }
    //解冻IP
    public function deleteIp(){
        //$this->CheckPower('7005');
        $pos= strpos($_SESSION['ltq_game_admin_user']['power'], '7004');
        if($pos === false){
            $this->ajaxReturn(0);
        }else{
            $this->ajaxReturn($pos);
        }
        $id = $_POST['data'];
       /* $data = $this->game("DELETE FROM data_confineaddress WHERE AddrString='$id'");

        if($data){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }*/
    }

    public function freeip(){
        if($_SESSION["ltq_game_admin_user"]['power']!='all'){
            $power='7004,';
            if (strpos($_SESSION["ltq_game_admin_user"]['power'], $power)===false) {
                $data['code'] = 0;
                $data['msg'] = '你没有相关权限，请联系管理员。。。';
                $this->ajaxReturn($data);
            }
        }
        $serverID = C('DATA_HOST');
        $ip = $_POST['ip'];
        //准备数组
        $command=array(
            'forbid'=>0,
            'forbidtype'=>4,
            'data'=>$ip
        );
        $command = json_encode($command);
        $rpcname="forbid_user";
        $time=date("Y-m-d H:i:s",time());
        $add['ServerIPAddr'] =$serverID;
        $add['Command'] =$command;
        $add['RpcName'] =$rpcname;
        $add['Time'] =$time;
        $commands = M('','','DB_CONFIG1');
        $re = $commands->table('data_command')->add($add);
        $data['code'] = 0;
        $data['msg'] = "冻结IP失败，IP地址为:".$ip;
        if($re){
            $data['code'] = 1;
            $data['msg'] = "冻结IP成功，IP地址为:".$ip;
        }
        $this->ajaxReturn($data);
    }

    //添加冻结IP
    public function getip(){
        $this->CheckPower('7003');
        $this->display('freezeIp');
    }

    public function getlistip(){
        S(array('type'=>'file','prefix'=>'disableIp','expire'=>60*60*24));
        $commands = M('','','DB_CONFIG1');
        $url = pageurl(__SELF__);
        $pagesize = empty($url['limit'])?10:$url['limit'];
        $page = empty($url['page'])?1:$url['page'];
        $state = ($page-1)*$pagesize;

        $list = S('disableIp_list_'.$page.$pagesize);
        $count = S('disableIp_count');
        $counts =$commands->table('data_confineaddress')->count();
        if($count < 0 || $count != $counts || empty($list)){
            $list = $commands->table('data_confineaddress')->limit($state,$pagesize)->select();
            S('disableIp_list_'.$page.$pagesize,$list);
            $count = $counts;
            S('disableIp_count',$count);
        }
        $data['message'] = '';
        $data['code'] = 0;
        $data['count'] = $count;
        $data['data'] = $list;
        $this->ajaxReturn($data,'json');
    }

    /*
     *  phonenem gameid 查询 要冻结者
     *
     */
    public function searchOne(){
        if(IS_POST) {
            $con = $_POST;
            $phonenum = trim($_POST['phonenum']); //用手机号查
            if ($phonenum) {
                $lists = $this->game("
SELECT accountid,nickname,gameid,disablelogin,phonenum,regtime 
FROM data_useraccount
WHERE phonenum=$phonenum
                ");
            }

            $gameid = trim($_POST['gameid']);//用gameid 查
            if ($gameid) {
                $lists2 = $this->game("
SELECT accountid,nickname,gameid,disablelogin,phonenum,regtime 
FROM data_useraccount
WHERE gameid=$gameid;
            ");

            }
            if(reset($lists)!=''AND reset($lists2)!=''){
                $lists = array_merge($lists, $lists2);
            }
            if(reset($lists)==''AND reset($lists2)!=''){
                $lists = $lists2;
            }
        }
        if(IS_GET){
            $id = $_GET['id'];  //accountid  select one
            if($id){
                $lists = $this->game("
SELECT accountid,nickname,gameid,disablelogin,phonenum,regtime 
FROM data_useraccount
WHERE accountid=$id;
                ");
                if(!$lists){
                    echo "game error!!";
                    die;
                }
            }elseif($_GET['type']=='add'){

            } 
        }
        $this->assign('lists',$lists);
        $this->assign('con',$con);
        $this->display("searchOne");
    }

    /*
     * 解冻
     *  修改游戏data_useraccount 表 的disablelogin字段
     * 写本来日志，disablelogin 表  一条记录 datetime accountid phonenum gameid nickname operator
     */
    public function nofree(){

        $cen = $this->getmsg($_GET['type']);
        if ($cen===false) {
            $data['code'] = 0;
            $data['msg'] = '你没有相关权限，请联系管理员。。。';
            $this->ajaxReturn($data);
        }
        $commands = M('','','DB_CONFIG1');
        $serverID = C('DB_HOST');
        $type = intval($_GET['type']);
        $id_int = intval($_GET['id']);
        if($type ==4){
            $id_int =$_GET['id'];
        }


        $command=array(
            'forbid'=>1,
            'forbidtype'=>$type,
            'data'=>$id_int
        );
        $command = json_encode($command);
        $rpcname="forbid_user";
        $time=date("Y-m-d H:i:s",time());
        $add['ServerIPAddr'] =$serverID;
        $add['Command'] =$command;
        $add['RpcName'] =$rpcname;
        $add['Time'] =$time;
        $result=  $commands->table('data_command')->add($add);
        if($result){
            if($type !=4){
                $weblog = M();
                $where['gameid'] = $id_int;
                $weblog->table('ltq_disablelogin')->where($where)->delete();
            }
            $data['code'] = 1;
            $data['msg'] = "解冻成功，".$id_int;
        }else{
            $data['code'] = 0;
            $data['msg'] = '操作失误，事物提交失败';

        }
        $this->ajaxReturn($data);
    }


    public function getmsg($type){
        $msg='';
        switch ($type){
            case '4':
                $msg="冻结IP成功，IP地址为:";
                break;
            case '8':
                if($_SESSION["ltq_game_admin_user"]['power']!='all'){
                    $power='3008,';
                    if (strpos($_SESSION["ltq_game_admin_user"]['power'], $power)===false) {
                        $data['code'] = 0;
                        $data['msg'] = '你没有相关权限，请联系管理员。。。';
                        $this->ajaxReturn($data);
                        return false;
                    }
                }
                $msg="解冻成功，gameid为:";
                break;
        }
        return  $msg;
    }


    /*
     * 解冻
     *  修改游戏data_useraccount 表 的disablelogin字段
     * 写本来日志，disablelogin 表  一条记录 datetime accountid phonenum gameid nickname operator
     */
    public function free(){
        $commands = M('','','DB_CONFIG1');
        $weblog = M();
        // $this->CheckPower('3008');
        if($_SESSION["ltq_game_admin_user"]['power']=='all'){

        }else{
            $power='7002,';
            if (strpos($_SESSION["ltq_game_admin_user"]['power'], $power)===false) {
                $data['code'] = 0;
                $data['msg'] = '你没有相关权限，请联系管理员。。。';
                $this->ajaxReturn($data);
            }
        }
      //  $this->CheckPower('7002');
        $gameid = $_REQUEST['id'];
        $user['disablelogin']='0';

        if($gameid > 0 && !empty($gameid)){
           // $re = $commands->table('data_useraccount')->where('gameid='.$gameid)->save($user);
            $result= $weblog->table('ltq_disablelogin')->where('gameid='.$gameid)->delete();
            if($result){
                $data['code'] = 1;
                $data['msg'] = "解冻成功，gameid为:$gameid";
            }else{
                $data['code'] = 0;
                $data['msg'] = "解冻失败，gameid为:$gameid";
            }
            $this->ajaxReturn($data);
        }

        $data['code'] = 0;
        //$data['data'] = $_REQUEST;
        $data['msg'] = "解冻失败，gameid为:空";

        $this->ajaxReturn($data);

       /* $condi=$_GET;
        $condi=array_merge($condi,$_POST);
        $limit = (int)50;
        $p=$condi['p']?(int)$condi['p']-1:(int)0;
        $skip=$skip*$limit;
        $this->CheckPower('3008');
        $disablelogin=$_GET['disablelogin'];
        $accountid = intval($_GET['accountid']);
        $id = "'".$accountid."'"; // dump($id);  dump($_SESSION);
        if($disablelogin==1 && $accountid>0){
            $data = $this->game("UPDATE data_useraccount SET disablelogin='0' WHERE accountid=$id");
            if($data===1){
                $lists=$this->game("SELECT accountid,account,gameid,nickname,disablelogin,phonenum FROM data_useraccount WHERE accountid=$id");
                //删除 一条 mysql log
                if(is_array($lists)){
                    $data= reset($lists);
                    $accountid = $data['accountid'];
                    if(intval($accountid)){
                        $result = $this->weblog("DELETE FROM ltq_disablelogin WHERE accountid =$accountid;
                        ");
                    }
                }
                $where=" where disablelogin=1 ";
                $list = $this->game("SELECT   * FROM data_useraccount  $where  limit $skip,$limit");
                $count=$this->game("SELECT  count(*) FROM data_useraccount  $where  ");
                $page_show = $this->page_show($count,$limit,$p);
                $this->assign("p",$condi['p']);
                $this->assign("count",$count);
                $this->assign("page_show",$page_show);
                $this->assign("condi",$condi);
                $this->assign('lists',$list);
                $this->display('index');
            }
        }*/
    }
}
